// pages/search/search.js
var api = require('../../utils/api.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
      page: 1,                   //初始页码
      pageSize: 10,
      hasMoreData: false,
      apipath: api.Apipath,
      list: [] 
    },
    getLists: function () {
      var that = this;
      var keyword = that.data.keyword;
      wx.showLoading({
        title: '加载中...',
        icon: 'loading',
        duration: 10000
      });
      wx.request({
        url: api.Search,
        data: { userId: wx.getStorageSync('userid'), role: wx.getStorageSync('role'), keyword: keyword, page: that.data.page, pageSize: that.data.pageSize },
        method: 'GET',
        success: function (res) {
          wx.hideLoading();
          var listTem = that.data.list;
          wx.request({
            url: api.SearchHistory,
            data: { userId: wx.getStorageSync('userid'), role: wx.getStorageSync('role') },
            method: 'GET',
            success: function (res) {
              if (res.data.code == 200) {
                var list = res.data.data;
                console.log(list);
                that.setData({
                  searchHistory: list,
                })
              }
            },
          })
          if (res.data.code == 200) {
            if (that.data.page == 1) {
              listTem = []
            }
            var list = res.data.data;
            console.log(list);
            if (list.length < that.data.pageSize) {
              that.setData({
                list: listTem.concat(list),
                hasMoreData: false
              })
            } else {

              that.setData({
                list: listTem.concat(list),
                hasMoreData: true,
                page: that.data.page + 1
              })

            }
          } else {
            wx.showToast({
              title: '暂时没有该医生',
              icon: 'none',
              duration: 2000
            })
          }
        },
      })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      var that = this;
      
      that.setData({ keyword: options.keyword })
      that.getLists();
    },
    getSuggest: function (event) {
      this.setData({
        keyword: event.detail.value
      })
    },
    search: function (e) {
      var that = this;
      var keyword = that.data.keyword;
      that.setData({ keyword: keyword, page: 1, pageSize: 10 })
      that.getLists();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function () {
      this.data.page = 1;
      this.getLists();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
      var that = this;
      console.log(that.data.hasMoreData);
      if (this.data.hasMoreData) {
        this.getLists();
      } else {
        wx.showToast({
          title: '暂无更多数据',
          icon: 'none'
        })
      }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})